package com.eportal.core;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;

public class myOracle {
	private static String strDriver = "oracle.jdbc.driver.OracleDriver";

	public static DataSource getDataSource(String ip, String port,
			String username, String pass, String db) {
		String strURL = "jdbc:oracle:thin:@" + ip + ":" + port + ":" + db;
		BasicDataSource ds = new BasicDataSource();
		ds.setDriverClassName(strDriver);
		ds.setUsername(username);
		ds.setPassword(pass);
		ds.setUrl(strURL);
		return ds;
	}

	public static void printDataSourceStats(DataSource ds) {
		BasicDataSource bds = (BasicDataSource) ds;
		System.out.println("NumActive: " + bds.getNumActive());
		System.out.println("NumIdle: " + bds.getNumIdle());
	}

	public static void shutdownDataSource(DataSource ds) {
		BasicDataSource bds = (BasicDataSource) ds;
		try {
			bds.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {
		try {
			String str = "洑勤华";
			byte [] s = str.getBytes();
			for(int i =0;i<s.length;i++){
				 System.out.print(s[i]+",");
				 
			 }
			System.out.println(" ");
			 Connection c = getDataSource("172.16.1.2", "1521", "pay20",
			 "pay20", "pep").getConnection();
			 PreparedStatement ps = c
					.prepareStatement("select bangdingcommercode, bangdingtermno, bangdingaccounts,substr(linkman,1,2) linkman from cs_sys_terminal where state = '1' and groupid = '057101' and bangdingtermno = '00600832' order by bangdingcommercode, bangdingtermno");
			ResultSet rset = ps.executeQuery();
			while (rset.next()) {
				 InputStream is = rset.getBinaryStream(4);
				 byte[] tmp = new byte[is.available()];
				 is.read(tmp);
				 System.out.println(new String(tmp));
				 for(int i =0;i<tmp.length;i++){
					 System.out.print(tmp[i]+",");
				 }
			}
			c.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
